package cn.itsource.pethome.dept.controller;

import cn.itsource.pethome.base.util.AjaxResult;
import cn.itsource.pethome.base.util.ResultBean;
import cn.itsource.pethome.dept.domain.Department;
import cn.itsource.pethome.dept.query.DepartmentQuery;
import cn.itsource.pethome.dept.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询部门" )
    public Department queryDepartment(@PathVariable("id")Long id){
        return departmentService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "返回部门树" )
    public List<Department> deptTree(){
        return departmentService.getTree();
    }

    /**
     * 高级查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "部门的高级查询" )
    public ResultBean<Department> queryDepartment(@RequestBody DepartmentQuery query){
        return departmentService.queryAll(query);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除部门" )
    public AjaxResult department(@PathVariable("id") Long id){
        try {
            departmentService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 添加和修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改部门 如果id为null则添加否则修改" )
    public AjaxResult department(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PatchMapping
    @ApiOperation(value = "部门的批量删除" )
    public AjaxResult dept(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 传统的写法
     * @param id
     * @return
     */
//    @RequestMapping("/getDepartmentById")
//    public Department getDepartmentById(Long id){
//        return departmentService.findById(id);
//    }

    //使用Restful 必须JSON数据
    //http://localhost:8080/dept/1/张三
//    @RequestMapping(value = "/{id}/{name}",method = RequestMethod.GET)
//    public Department queryDepartment(@PathVariable("id") Long id,@PathVariable("name") String name){
//        return departmentService.findById(id);
//    }
}
